package com.treasure.service.loan.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.HttpClientErrorException;
import org.springframework.web.client.RestTemplate;

import com.treasure.service.BaseService;
import com.treasure.service.dto.LoanDto;
import com.treasure.service.dto.TLoanOrderDto;
import com.treasure.service.third.utils.RestClient;
import com.treasure.service.result.PageResult;
import com.treasure.service.result.ResponseResult;

import lombok.extern.slf4j.Slf4j;

@Service("loanService")
@Slf4j
public class LoanServiceImpl extends BaseService implements LoanService {
    @Autowired
    private RestTemplate restTemplate;
    @Autowired
    private RestClient restClient;
	@Override
	public PageResult<List<LoanDto>> getLoans() {
		MultiValueMap<String, Object> map = new LinkedMultiValueMap();
		map.set("pageSize","1");
		map.set("pageNum","1");
		map.set("userId","");
		map.set("param","");

		HttpHeaders httpHeaders = new HttpHeaders();
		HttpEntity<MultiValueMap<String, Object>> formEntity = new HttpEntity<MultiValueMap<String, Object>>(map, httpHeaders);
		ParameterizedTypeReference<PageResult<List<LoanDto>>> pt = new ParameterizedTypeReference<PageResult<List<LoanDto>>>() {
		};
		try {
			ResponseEntity<PageResult<List<LoanDto>>> resp = restTemplate.exchange(this.getGateway() + "/loans/v1/getLoanList",HttpMethod.POST, formEntity, pt);
			return resp.getBody();
		} catch (HttpClientErrorException e) {
			log.error("请求方法 "+this.getGateway() + "/order/v1/getCardOrderList statusCode : {} error:{} ", e.getStatusCode(), e);
		}
		return null;
	}

	@Override
	public String createOrder(TLoanOrderDto tLoanOrderDto) {
		ResponseResult result = restClient.post(this.getGateway()+"/loans/v1/createOrder",tLoanOrderDto,ResponseResult.class);
		return result.getData().toString();
	}

}
